package industry;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.player.PlayerAnimationEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import custom.Tool;

import actives.ActiveManager;
import actives.ActiveType;
import main.Plugin;

public class IndustryHandler implements Listener {

	public IndustryHandler(Plugin plugin) {
		Bukkit.getPluginManager().registerEvents(this, plugin);
	}

	@EventHandler
	private void industryBuff(BlockDamageEvent event) {
		Player player = event.getPlayer();
		Block block = event.getBlock();
		switch (Tool.getTool(player.getItemInHand())) {
		case AXE:
			if (Woodcutting.isLoggable(block)) {
				Industry.applyBuff(player, block);
			}
			break;
		case PICKAXE:
			if (Mining.isMinable(block)) {
				Mining.active(player, block);
				Industry.applyBuff(player, block);
			}
			break;
		case SPADE:
			if (Archaeology.isDiggable(block)) {
				Archaeology.active(player, block);
				Industry.applyBuff(player, block);
			}
			break;
		case HOE:
			if (Farming.isCrop(block)) {
				Farming.active(player, block);
				Industry.applyBuff(player, block);
			}			
			break;
		default:
			break;
		}
	}

	@EventHandler
	private void refreshIndustryBuff(PlayerAnimationEvent event) {
		Player player = event.getPlayer();
		Block block = player.getTargetBlock(null, 5);
		switch (Tool.getTool(player.getItemInHand())) {
		case AXE:
		case PICKAXE:
		case SPADE:
		case HOE:
			Industry.refreshBuff(player, block);
			break;
		default:
			break;
		}
	}

	@EventHandler
	private void industryActives(PlayerInteractEvent event) {
		if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
			Player player = event.getPlayer();
			Block block = event.getClickedBlock();
			switch (Tool.getTool(player.getItemInHand())) {
			case AXE:
				if (Woodcutting.isLoggable(block)) {
					ActiveManager.prime(player, ActiveType.WOODCUTTING);
				}
				break;
			case PICKAXE:
				if (Mining.isMinable(block)) {
					ActiveManager.prime(player, ActiveType.MINING);
				}
				break;
			case SPADE:
				if (Archaeology.isDiggable(block)) {
					ActiveManager.prime(player, ActiveType.ARCHAEOLOGY);
				}
				break;
			default:
				break;
			}
		}
	}
}
